#include <bits/stdc++.h>
#define debug cerr << "\33[33m[" << __LINE__ << "]\33[m "
#define SZ(x) ((int) x.size() - 1)
#define all(x) x.begin(), x.end()
#define ms(x, y) memset(x, y, sizeof x)
#define F(i, x, y) for (int i = (x); i <= (y); ++i)
#define DF(i, x, y) for (int i = (x); i >= (y); --i)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T> T& chkmax(T& x, T y) { return x = max(x, y); }
template <typename T> T& chkmin(T& x, T y) { return x = min(x, y); }
template <typename T> T& read(T& x) {
    x = 0; int f = 1; char c = getchar();
    for (; !isdigit(c); c = getchar()) if (c == '-') f = - 1;
    for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
    return x *= f;
}
const int N = 2e5 + 10, M = 2e6 + 10, T = 2e6;
const ll INF = 1e18;
int n, a[N];
ll f[M], d[M];
void zhk() {
    read(n);
    F(i, 1, n) read(a[i]);
    F(i, 1, T) f[i] = - INF, d[i] = 0;
    ll mx = 0, s = 0;
    F(i, 1, n) {
        ll w = max(mx, f[a[i]] + a[i] + s - d[a[i]]);
        if (a[i] == a[i - 1]) {
            mx += a[i];
            s += a[i];
        }
        if (i > 1) {
            chkmax(mx, chkmax(f[a[i - 1]] += s - d[a[i - 1]], w));
            d[a[i - 1]] = s;
        }
    }
    cout << mx << '\n';
}
signed main() {
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    int _; read(_);
    while (_--) zhk();
    return 0;
}